package com.webserver.controller;


import com.sun.xml.internal.ws.api.message.HeaderList;
import com.webserver.core.annotations.Controller;
import com.webserver.core.annotations.RequertMapping;
import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.Article;
import com.webserver.vo.User;

import java.io.*;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
@Controller
public class ArticleController {
    private static String articleDirName="./article/";
    static {
        File userFile = new File(articleDirName);
        if (!userFile.exists()) {
            userFile.mkdirs();
        }
    }

@RequertMapping("/myweb/writeArticle")
    public void writeArticle(HttpRequest request, HttpResponse response){
        String title=request.getParameter("title");
        String content=request.getParameter("content");

        if (title==null||content==null){
            response.setEntity(new File("./webapps/myweb/article_fail.html"));
            return;
        }

        try(
                ObjectOutputStream oos = new ObjectOutputStream(
                        new FileOutputStream(articleDirName+title+".obj")
                );
        ){
            Article article = new Article(title,content);
            oos.writeObject(article);
            response.setEntity(new File("./webapps/myweb/article_success.html"));

        }catch(IOException e){
            e.printStackTrace();
        }

        System.out.println("用户注册处理完毕!!!!");
    }

@RequertMapping("/myweb/showAllArticle")
    public void showAllArticle(HttpRequest request, HttpResponse response){
        List<Article>arrayList=new ArrayList();
        File file=new File(articleDirName);
        File[] files = file.listFiles(e->e.getName().endsWith(".obj"));
        for (File file1:files){
            try {
                ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file1));
                arrayList.add((Article)ois.readObject());
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
            arrayList.forEach(System.out::println);
        }
        PrintWriter pw = response.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println(" <meta charset=\"UTF-8\">");
        pw.println(" <title>用户列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>文章列表</h1>");
        pw.println(" <table border=\"1\" cellpadding=\"0\" cellspacing=\"0\">");
        pw.println("<tr align=\"center\">");
        pw.println("<td>标题</td>");


        pw.println("</tr>");
        for (Article article : arrayList) {
            pw.println("<tr>");
            pw.println("<td>" + article.getTitle() + "</td>");
            pw.println("<td>" + article.getContent() + "</td>");

            pw.println("</tr>");

        }

        pw.println("</table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");
        //设置正文类型
        response.setContentType("text/html");
    }



}
